<?php
declare (strict_types=1);

namespace app\model\communal;

use app\constant\LogicConstant;
use app\facade\StringFacade;
use app\model\concern\AmisTrait;
use think\db\Query;
use think\Model;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;
use think\model\relation\HasOneThrough;
use think\model\relation\MorphOne;

class UserModel extends Model
{
    use AmisTrait;

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '55000_user';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * 软删除
     */
    use SoftDelete;

    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = 0;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'admin_id', 'name', 'age', 'gender', 'avatar', 'mobile', 'type', 'state', 'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'name', 'gender', 'mobile', 'type', 'state', 'createtime',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = [
        'name', 'gender', 'avatar', 'mobile', 'type', 'state',
    ];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = [
        'id', 'name', 'gender', 'age', 'avatar', 'mobile', 'type', 'state',
    ];

    /**
     * @var string[]
     */
    public static $itemColumn = ['id', 'state'];

    /**
     * 修改
     *
     * @param $value
     * @return string|string[]
     */
    public function setAvatarAttr($value)
    {
        return StringFacade::replaceDomainShowUrl($value);
    }

    /**
     * avatar 获取
     *
     * @param $value
     * @param $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        if (empty($value)) {
            // 默认头像
            $value = config('domain.api') . '/static/web/images/default_avatar.png';
        } else {
            $value = StringFacade::splicingDomainShowUrl($value);
        }

        return $value;
    }

    /**
     * name 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchNameAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('name', 'like', '%' . $value . '%');
        }
    }

    /**
     * mobile 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchMobileAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('mobile', 'like', '%' . $value . '%');
        }
    }

    /**
     * type 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchTypeAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('type', $value);
        }
    }

    /**
     * 授权密码
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * 验证字段名
     *
     * @return string
     */
    public function getAuthIdentifierName()
    {
        return 'id';
    }

    /**
     * auth field
     *
     * @return array
     */
    public function getAuthVisibleFields()
    {
        return ['id', 'name', 'mobile', 'type', 'state'];
    }

    /**
     * token generate data
     *
     * @return array
     */
    public function getAuthTokenGenerateData()
    {
        $unique = $this->getAuthIdentifierName();
        return [
            $unique => $this->$unique,
        ];
    }

    /**
     * 启用禁用状态
     *
     * @return bool
     */
    public function validateState()
    {
        if (LogicConstant::STATE_ONE == $this->state) {
            return true;
        }
        return false;
    }

    /**
     * 关联附表
     *
     * @return HasOne
     */
    public function token()
    {
        return $this->hasOne(UserTokenModel::class, 'uid', 'id')
            ->bind([
                'token' => 'token'
            ]);
    }
}
